#include "games.h"

Games::Games(QString name)
{
    QSqlDatabase sqldb = QSqlDatabase::addDatabase("QMYSQL");
    sqldb.setHostName("localhost");
    sqldb.setDatabaseName("uselessdb");
    sqldb.setUserName("root");
    sqldb.setPassword("");
    if (!sqldb.open())
    {
        QMessageBox::critical(new QWidget(), "Error connection", "Unable to connect to SQL Server.");
        throw 4242;
    }
    else
    {
        QSqlQuery requete;
        requete.exec("SELECT * FROM game");
        while (requete.next())
            if (requete.value("name").toString() == name)
                initialise(requete);
    }
}

Games::~Games() {}

void Games::initialise(QSqlQuery &requete)
{
    m_path = requete.value("path_install").toString();
    m_name = requete.value("name").toString();
    m_version = requete.value("version").toString();
    QString os = requete.value("operating_system").toString();
    if (os.size() == 3)
    {
        m_os.insert("linux", (os[0] == '1'));
        m_os.insert("windows", (os[1] == '1'));
        m_os.insert("mac", (os[2] == '1'));
    }
}

QString Games::getPath()
{
    return (m_path);
}

QString Games::getName()
{
    return (m_name);
}

bool    Games::getInstall()
{
    return (m_isInstall);
}

bool    Games::getPaid()
{
    return (m_isPaid);
}

int     Games::getSizeKo()
{
    return (m_sizeKo);
}

QString Games::getVersion()
{
    return (m_version);
}

bool    Games::getOs(QString os)
{
    if (os == "linux" || os == "windows" || os == "mac")
        return (m_os[os]);
    return (false);
}

void    Games::setPath(QString path)
{
    m_path = path;
}

